package com.marvels.loanapply.consum.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.collection.api.LoanAccountApi;
import com.marvels.financial.loan.api.FinancialCheckBillApi;
import com.marvels.loanapply.consum.LoanapplyConsumerFinancialApi;
import com.marvels.loanapply.dto.CollectAccountDTO;
import com.marvels.loanapply.dto.CommonConditionDTO;
import com.marvels.loanapply.dto.McfeCustomerRepaymentPlanDTO;

import cn.hutool.core.bean.BeanUtil;

@Component
public class LoanapplyConsumerFinancialApiImpl implements LoanapplyConsumerFinancialApi {

	@Autowired
	private LoanAccountApi loanAccountApi;
	
	@Autowired
	private FinancialCheckBillApi checkBillApi;
	
	@Override
	public PageInfo<McfeCustomerRepaymentPlanDTO> planList(CommonConditionDTO dto) throws MarvelsBusinessException {
		try {
			com.marvels.financial.collection.dto.CommonConditionDTO param = new com.marvels.financial.collection.dto.CommonConditionDTO();
			BeanUtil.copyProperties(dto, param);
			PageInfo<com.marvels.financial.loan.dto.McfeCustomerRepaymentPlanDTO> apiResult = loanAccountApi.planList(param);
			PageInfo<McfeCustomerRepaymentPlanDTO> result = (PageInfo<McfeCustomerRepaymentPlanDTO>) 
					JSON.parseObject(JSONObject.toJSONString(apiResult), new TypeReference<PageInfo<McfeCustomerRepaymentPlanDTO>>(){});
			MarvelsLogUtil.getInstance().info("LoanapplyConsumerFinancialApi.planList direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("LoanapplyConsumerFinancialApi.planList error", e);
			return null;
		}
	}

	@Override
	public CollectAccountDTO selectAccountInfo(String loanId) throws MarvelsBusinessException {
		try {
			com.marvels.financial.collection.dto.CollectAccountDTO apiResult = loanAccountApi.selectAccountInfo(loanId);
			CollectAccountDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), CollectAccountDTO.class);
			MarvelsLogUtil.getInstance().info("LoanapplyConsumerFinancialApi.selectAccountInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("LoanapplyConsumerFinancialApi.selectAccountInfo error", e);
			return null;
		}
	}

	@Override
	public Double selectReturnTotalMoney(String loanIds) throws MarvelsBusinessException {
		try {
			Double result = checkBillApi.selectReturnTotalMoney(loanIds);
			MarvelsLogUtil.getInstance().info("LoanapplyConsumerFinancialApi.selectReturnTotalMoney direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("LoanapplyConsumerFinancialApi.selectReturnTotalMoney error", e);
			return null;
		}
	}

}
